<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>音频录制</title>
    <style>
        .app {display: flex;justify-content: center; align-items: center;}
        .record-btn {margin: 0 10px;}
    </style>
</head>

<body>
    <div class="app">
        <audio controls class="audio-player"></audio>
        <button class="record-btn">录音</button>
    </div>
</body>
<script>
    const recordBtn = document.querySelector(".record-btn")
    const player = document.querySelector(".audio-player")
    let chunks = []
    const constraints = { audio: true }
    const ws=new WebSocket('ws://127.0.0.1:9999/upgrade')
    ws.onmessage = evt =>{
        window.URL.createObjectURL(evt.data)
        player.src = audioURL
    }
    navigator.mediaDevices.getUserMedia(constraints).then(
        stream => {
            const mediaRecorder = new MediaRecorder(stream)
            recordBtn.onclick = () => {
                if (mediaRecorder.state === "recording") {
                    mediaRecorder.stop()
                    recordBtn.textContent = "录音结束"
                } else {
                    mediaRecorder.start()
                    recordBtn.textContent = "录音中..."
                }
            }
            mediaRecorder.ondataavailable = e => {
                chunks.push(e.data)
            }
            mediaRecorder.onstop = e => {
                const blob = new Blob(chunks, { type: "audio/ogg; codecs=opus" })
                chunks = []
                
                ws.send(blob)
            }
        }
    )
</script>

</html>